package com.hmdp.utils;

import cn.hutool.captcha.LineCaptcha;
import com.hmdp.entity.Captcha;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author ZhongTB
 * @create 2023-07-11 9:36
 */
@Component
public class CaptchaUtil {

    @Value("18000")
    private long expireTime;

    @Value("200")
    private int width;
    @Value("400")
    private int height;
    @Value("300")
    private int length;

    public Captcha createCode() {
        // 创建验证码图片
        LineCaptcha captcha = cn.hutool.captcha.CaptchaUtil.createLineCaptcha(width, height, length, 20);
        // 生成验证码
        String code = captcha.getCode();
        // 保存验证码
        Captcha captcha1 = new Captcha(code, expireTime);

        return captcha1;
    }

    public boolean checkCode(String userCode, Captcha captcha){
        if(captcha == null || captcha.isExpired()){
            return false;
        }
        if(!captcha.getCode().equalsIgnoreCase(userCode.trim())){
            return false;
        }

        return true;
    }
}
